package game.players.cpu.opponent;

import com.uqbar.vainilla.appearances.Sprite;

import game.ball.Possessed;
import game.ball.SoccerBall;
import game.players.SoccerPlayer;
import game.players.states.WithoutBallSweepingPlayer;

public class WithoutBallSweepingCPUOpp extends WithoutBallSweepingPlayer {

	public WithoutBallSweepingCPUOpp(SoccerPlayer player, boolean isRight) {
		super(player, isRight);
	}
	
	@Override
	protected void setRightSprite() {
		this.getPlayer().setAppearance(Sprite.fromImage("/images/goal3_barrida3.png").scale(1.5));
	}
	
	@Override
	protected void setLeftSprite() {
		this.getPlayer().setAppearance(Sprite.fromImage("/images/goal3_barrida3.png").scale(1.5).flipHorizontally());
	}
	
	protected void takeTheBall(SoccerBall ball) {
		double advanceX = this.getPlayer().getX() - ball.getX() + ball.getRatio();
		double advanceY = this.getPlayer().getY() + this.getPlayer().getAppearance().getHeight() - ball.getY() - ball.getRatio();
		
		ball.moveNormally(advanceX, advanceY);
	}
	
	public void snatchTheBall() {
		this.getPlayer().setState(new WithBallSweepingCPUOpp(this.getPlayer(), this.isRight(), this.getTimerTask()));
		this.getPlayer().getScene().changeBallPossessionTo(this.getPlayer());
	}
	
	public void takePossession(SoccerBall ball) {
		this.takeTheBall(ball);
		ball.setState(new Possessed(this.getPlayer()));
		this.getPlayer().setState(new WithBallSweepingCPUOpp(this.getPlayer(), this.isRight(), this.getTimerTask()));
	}
	
	public void finishSweep() {
		this.getUpPlayer();
		if (this.getPlayer().isWithBall())
			this.getPlayer().setState(new WithBallLeftMovingCPUOpp(this.getPlayer(), -this.getPlayer().getMovementSpeed(), 0));
		else
			this.getPlayer().setState(new WithoutBallStationaryCPUOpp(this.getPlayer()));
	}

}
